Imports ADOConnection
Imports System.Data.SqlClient

Public Class frmNhapLoaiThuoc
    'auto updatedata use event Changing_Column

    Public fCallVattu As Form
    Private m_objLoaiThuoc As CLoaiThuoc

    Private daLoaiThuoc As SqlDataAdapter
    Private daThuoc As SqlDataAdapter
    Private dsLoaiThuoc As New DataSet
    Private FSave As Boolean

    Public Sub New(ByVal objLoaiThuoc As CLoaiThuoc)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        m_objLoaiThuoc = objLoaiThuoc
    End Sub

    Private Sub frmNhapLoaiThuoc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        KhoiTaodtgLoaiThuoc()
        txtTenLoaiThuocCha.Text = m_objLoaiThuoc.TenLoaiThuoc
        FSave = False
        '
        AddHandler dsLoaiThuoc.Tables(0).ColumnChanging, New _
                        DataColumnChangeEventHandler(AddressOf Column_Changing)
    End Sub

    Private Sub Column_Changing(ByVal sender As Object, ByVal e As DataColumnChangeEventArgs)
        If (FSave) Then
            Exit Sub
        End If
        If (e.Column.ColumnName = "ma_loaithuoc") Then
            '
            Dim ma_loaithuoc As String
            ma_loaithuoc = e.ProposedValue
            'hieu chinh du lieu neu sua vao cot ma_loaithuoc
            '2Truong Hop:
            'Ma o muc cha va Ma o muc con.

            If (ma_loaithuoc.Length <> 14) Then
                ShowMessages("Chiều dài chuỗi không hợp lệ")
                e.ProposedValue = e.Row("ma_loaithuoc")
                Exit Sub
            End If
            Dim strsql As String
            strsql = "select count(*) from tblLoaiThuoc where ma_loaithuoc = '" & ma_loaithuoc & "'"
            Dim value As Integer
            value = BaseDB.ExecSql_DataValue(strsql)
            If (value <> 0) Then
                ShowMessages("Đã tồn tại mã loại thuốc này")
                e.ProposedValue = e.Row("ma_loaithuoc")
                Exit Sub
            End If
            If (Not e.Row("ma_loaithuoc") Is DBNull.Value) Then
                'truong hop sua ma.
                Dim ma_loaithuoccu, ma_loaithuocmoi As String
                ma_loaithuoccu = e.Row("ma_loaithuoc")
                ma_loaithuocmoi = e.ProposedValue

                'cap nhat lai LoaiThuoc sau khi sua
                Dim objLoaiThuoc As CLoaiThuoc
                objLoaiThuoc = CLoaiThuocs.GetLoaiThuocByMa(ma_loaithuoccu)
                CLoaiThuocs.UpdateLoaiThuoc(objLoaiThuoc, ma_loaithuocmoi)
                FSave = True
                UpdateData()

                '2TH: LoaiThuoc o Muc Cha va LoaiThuoc o Muc Con
                strsql = "select count(*) from tblDMThuoc where maloaithuoc = '" & ma_loaithuoccu & "'"
                value = BaseDB.ExecSql_DataValue(strsql)
                If (value <> 0) Then
                    'o Muc con
                    'cap nhat bang DMThuoc voi maloaithuoc thay doi.
                    Dim aryThuoc As ArrayList = CThuocs.GetThuocListByLoaiChaID(ma_loaithuoccu)
                    For Each obj As CThuoc In aryThuoc
                        'cap nhat loai maloaithuoc.
                        'gan lai gia tri moi
                        obj.MaLoaiThuoc = ma_loaithuocmoi
                        CThuocs.UpdateThuoc(obj)
                    Next
                Else
                    'cap nhat bang DMLoaiThuoc voi ma cha thay doi ung voi cac loaithuoc con
                    Dim aryLoaiThuoc As ArrayList = CLoaiThuocs.GetLoaiThuocListByMaCha(ma_loaithuoccu)
                    For Each obj As CLoaiThuoc In aryLoaiThuoc
                        'cap nhat loai maloaithuoc.
                        'gan lai gia tri moi
                        obj.MaCha = ma_loaithuocmoi
                        CLoaiThuocs.UpdateLoaiThuoc(obj)
                        FSave = True
                        UpdateData()
                    Next
                End If
            Else
                'truong hop them moi.
                'insert new row in tblDMLoaiThuoc
                If (Not e.Row("ten_loaithuoc") Is DBNull.Value) Then
                    Dim objLT As New CLoaiThuoc
                    objLT.MaLoaiThuoc = e.ProposedValue
                    objLT.MaCha = m_objLoaiThuoc.MaLoaiThuoc
                    objLT.TenLoaiThuoc = e.Row("ten_loaithuoc")
                    CLoaiThuocs.InsertLoaiThuoc(objLT)
                    FSave = True
                    UpdateData()
                End If
            End If
        ElseIf (e.Column.ColumnName = "ma_cha") Then
            'occur when btnLoaiThuoc_Click
            'moi them vao ko can cap nhat
        ElseIf (e.Column.ColumnName = "ten_loaithuoc") Then
            'chia lam 2 TH:  ten_loaithuoccu= NULL va ten_loaithuoccu <>NULL 
            'sua cot ten loai thuoc
            'update loaithuoc voi ten moi
            'khac Null thi Update
            If (Not e.Row("ma_loaithuoc") Is DBNull.Value) Then
                Dim ten_loaithuoccu, ten_loaithuocmoi As String
                If (Not e.Row("ten_loaithuoc") Is DBNull.Value) Then
                    'truong hop sua ten loai thuoc
                    ten_loaithuoccu = e.Row("ten_loaithuoc")
                    ten_loaithuocmoi = e.ProposedValue
                    Dim objLoaiThuoc As CLoaiThuoc = CLoaiThuocs.GetLoaiThuocByTen(ten_loaithuoccu)
                    objLoaiThuoc.TenLoaiThuoc = ten_loaithuocmoi
                    CLoaiThuocs.UpdateLoaiThuoc(objLoaiThuoc)
                    FSave = True
                    UpdateData()
                Else
                    'truong hop them moi
                    Dim objLT As New CLoaiThuoc
                    objLT.MaLoaiThuoc = e.Row("ma_loaithuoc")
                    objLT.MaCha = m_objLoaiThuoc.MaLoaiThuoc
                    objLT.TenLoaiThuoc = e.ProposedValue
                    CLoaiThuocs.InsertLoaiThuoc(objLT)
                    FSave = True
                    UpdateData()
                End If
            Else
                'ShowMessages("Bạn phải nhập mã trước.")
                'e.ProposedValue = DBNull.Value
            End If
        End If
        'load new data frmVatTu

    End Sub

    Private Sub dtgLoaiThuoc_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgLoaiThuoc.CellEndEdit
        FSave = False
    End Sub

    'public function shared Kiemtra

#Region "dtgLoaiThuoc"
    Public Sub KhoiTaodtgLoaiThuoc()
        'Dim objConnection As IADOConnection = g_objConnFactory.GetConnection
        Dim strsql As String = "select * from tblLoaiThuoc where ma_cha ='" & m_objLoaiThuoc.MaLoaiThuoc & "'"
        daLoaiThuoc = New SqlDataAdapter(strsql, BaseDB.sqlConnect.ConnectionString)
        Dim sqlcommbuilder As New SqlCommandBuilder(daLoaiThuoc)
        dsLoaiThuoc.Clear()
        daLoaiThuoc.Fill(dsLoaiThuoc, "tblLoaiThuoc")
        dtgLoaiThuoc.DataSource = dsLoaiThuoc.Tables("tblLoaiThuoc")
        FormatGridStyleLoaiThuoc()
        'khoi tao dtgThuoc
        'strsql = "select * from tblDMThuoc"
        'daThuoc = New SqlDataAdapter(strsql, BaseDB.sqlConnect.ConnectionString)
        'daThuoc.Fill(dsLoaiThuoc, "tblThuoc")
        ''Dim rel As DataRelation = dsLoaiThuoc.Relations.Add("LoaiThuoc_Thuoc", dsLoaiThuoc.Tables("tblLoaiThuoc").Columns("ma_loaithuoc"), dsLoaiThuoc.Tables("tblThuoc").Columns("MaLoaiThuoc"))
        'dtgThuoc.DataSource = dsLoaiThuoc.Tables("tblThuoc")


    End Sub

    Public Sub FormatGridStyleLoaiThuoc()
        With dtgLoaiThuoc
            'Rename(HeaderText)
            With .Columns.Item("ma_loaithuoc")
                .HeaderText = "Mã"
                .Width = 100
            End With
            With .Columns.Item("ten_loaithuoc")
                .HeaderText = "Loại Thuốc"
                .Width = 300
            End With
            With .Columns.Item("ma_cha")
                .HeaderText = ""
                .ReadOnly = True
                .Visible = False
            End With
        End With
    End Sub
#End Region

#Region "btnLoaiThuoc"
    Private Sub btnLoaiThuoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoaiThuoc.Click
        'them loai thuoc vao dsLoaiThuoc
        Dim row As DataRow
        row = dsLoaiThuoc.Tables("tblLoaiThuoc").NewRow
        row("ma_cha") = m_objLoaiThuoc.MaLoaiThuoc
        dsLoaiThuoc.Tables("tblLoaiThuoc").Rows.Add(row)
    End Sub

    'Private Sub btnCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhat.Click
    '    UpdateData()
    'End Sub

    Public Sub UpdateData()
        'refesh data in frmVatTu
        Try
            Dim frm As frmVatTu = CType(fCallVattu, frmVatTu)
            frm.RefreshCurrentNode()
            frm.LoadDMLoaiThuoc()
            frm.LoaddsThuoc()
            'refesh ds trong frmVatTu
        Catch ex As Exception
        End Try
    End Sub
#End Region
End Class